from django.views import View
from django.shortcuts import render
import collections
from web import models
from django.db.models import Count
from django.http import JsonResponse

class StatisticsView(View):
    '''
    统计页面视图
    '''
    def get(self,request,project_id):
        return render(request,'web/statistics.html')


class StatisticsPriority(View):
    '''
    按照优先级生成饼图
    '''
    def get(self,request,project_id):
        start=request.GET.get('start')
        end=request.GET.get('end')
        data_dict=collections.OrderedDict()
        for key,text in models.Issues.priority_choices:
            data_dict[key]={'name':text,'y':0}
        #
        result=models.Issues.objects.filter(project_id=project_id,create_datetime__gte=start,create_datetime__lte=end).\
            values('priority').annotate(ct=Count('id'))
        for item in result:
            data_dict[item['priority']]['y']=item['ct']
        return JsonResponse({'status':True,'data':list(data_dict.values())})

class StatisticsProjectUser(View):
    '''
    项目中每个成员分配的任务图的 获取的 视图
    '''
    def get(self,request,project_id):
        start=request.GET.get('start')
        end=request.GET.get('end')
        all_user_dict=collections.OrderedDict()
        all_user_dict[request.tracer.project.creator_id]={
            'name':request.tracer.project.creator.username,
            'status':{item[0]:0 for item in models.Issues.status_choices}
        }
        all_user_dict[None]={
            'name':'未指派',
            'status':{item[0]:0 for item in models.Issues.status_choices}
        }
        user_list=models.ProjectUser.objects.filter(project_id=project_id)
        for item in user_list:
            all_user_dict[item.user_id]={
                'name':item.user.username,
                'status':{item[0]:0 for item in models.Issues.status_choices}
            }
        issues=models.Issues.objects.filter(project_id=project_id,create_datetime__gte=start,create_datetime__lte=end)
        for item in issues:
            if not item.assign:
                all_user_dict[None]['status'][item.status]+=1
            else:
                all_user_dict[item.assign_id]['status'][item.status]+=1
        # print(all_user_dict)
        #OrderedDict([(2, {'name': 'strivehe', 'status': {1: 1, 2: 1, 3: 0, 4: 0, 5: 0, 6: 0, 7: 0}}), (None, {'name': '未指派', 'status': {1: 6, 2: 0, 3: 1, 4: 1, 5: 1, 6: 0, 7: 0}})
        categories=[data['name'] for data in all_user_dict.values()] #['strivehe', '未指派', 'heximin']
        data_result_dict=collections.OrderedDict()
        for item in models.Issues.status_choices:
            data_result_dict[item[0]]={'name':item[1],'data':[]}
        #OrderedDict([(1, {'name': '新建', 'data': []}), (2, {'name': '处理中', 'data': []}), (3, {'name': '已解决', 'data': []}),

        for key,text in models.Issues.status_choices:
            for row in all_user_dict.values():
                count=row['status'][key]
                data_result_dict[key]['data'].append(count)
        context={
            'status':True,
            'data':{
                'categories':categories,
                'series':list(data_result_dict.values())
            }
        }
        return JsonResponse(context)